//
//  CHSListTableController.m
//  CAIImagePickerController_Example
//
//  Created by 海参 on 2022/8/4.
//  Copyright © 2022 海参. All rights reserved.
//  静态tableView例子

#import "CHSListTableController.h"
#import <CAIImagePickerControllerKit/CAIImagePickerControllerKit.h>
#import <CAIImagePickerControllerKit/CAICameraOverlayView.h>
#import <ZXCategoriesUtil/CAICategories.h>

@interface CHSListTableController ()<CAIPHPickerControllerManagerDelegate,CAIImagePickerControllerKitDelegate,
ZXUIImagePickerManagerDelegate,CAICameraOverlayViewDelegate>

@property (nonatomic, strong) ZXUIImagePickerManager *imagePickerVCManager;

@property (nonatomic, strong) CAIPHPickerControllerManager *pickerControllerManager API_AVAILABLE(ios(14.0));

@property (nonatomic, strong) CAIImagePickerControllerKit *imagePickerControllerKit;

@property (weak, nonatomic) IBOutlet UISwitch *uiImagePickerSwitch;
///自定义相机最多拍摄数量
@property (weak, nonatomic) IBOutlet UITextField *customCameraMaxPhotoTextField;

@end

@implementation CHSListTableController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    NSLog(@"%f",[NSDate currentTickMediaTime]);
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.customCameraMaxPhotoTextField.text = @"1";
    
    // Uncomment the following line to preserve selection between presentations.
     self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSInteger rows = 0;
    switch (section) {
        case 0:rows =1;break;
        case 1:rows =1;break;
        case 2:rows =7;break;
        case 3:rows =2;break;
        default:
            break;
    }
    return rows;
}

/*
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<#@"reuseIdentifier"#> forIndexPath:indexPath];
    
    // Configure the cell...
    
    return cell;
}
*/

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return  UITableViewAutomaticDimension;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.1;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return @"TZImagePickerController组件";
    }
    else if (section == 1) {
        return @"系统新组件：PHPicker";
    }
    if (section == 2) {
        return @"ZXUIImagePickerManager组件：二次封装UIImagePickerController，包括选择照片，拍照，自定义拍照";
    }
    return @"最终全部包含封装的组件：CAIImagePickerController";
}

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section{
    UITableViewHeaderFooterView *headerView = (UITableViewHeaderFooterView *)view;
    headerView.textLabel.numberOfLines = 0;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0){
       //静态tableView直接执行跳转到下一页
    }
    else if (indexPath.section == 1){
        [self phPickerAction];
    }
    //自定义UIImagePickerController
    else if (indexPath.section == 2){
        switch (indexPath.row) {
                //选择照片
            case 0: [self uiImagePickerPresentPhotoLibrary];
                break;
                //单拍照片
            case 1: [self uiImagePickerPresentCamera];
                break;
                //自定义多拍照片
            case 2: [self uiImagePickerPresentCameraMultiPhotoAction];
                break;
                //单拍视频
            case 3: [self uiImagePickerCameraMovieAction];
                break;
                //展示自定义相机的OverlayView
            case 5: [self showCameraOverlayView];
                break;
            default:
                break;
        }
    }
    else if (indexPath.section == 3){
        
        switch (indexPath.row) {
            case 0:
                [self mySDKPickerPhotoAction];
                break;
            case 1:
                [self mySDKCameraAction];
                break;
            default:
                break;
        }
    }
}




#pragma mark - CAIImagePickerControllerKit

- (void)mySDKPickerPhotoAction{
    [self.imagePickerControllerKit showImagePickerWithFromViewController:self delegate:self maxImagesCount:3 allowPickingOriginalPhoto:YES allowTakePicture:NO];
}


- (void)mySDKCameraAction{
    [self.imagePickerControllerKit showCameraControllerWithSourceController:self delegate:self];
}

- (void)imagePickerController:(nullable CAIImagePickerControllerKit *)picker
         didFinishPickingPhotos:(nullable NSArray<UIImage *> *)photos
                   sourceAssets:(nullable NSArray *)assets
                        error:(nullable NSError *)error{
    if (photos && photos.count >0) {
//        UIImage *image = photos.firstObject;
//        UIImage *img = [UIImage zx_resizeImage:image];
//        [self.selectedPhotos addObject:img];
        
//        [self performSelector:@selector(test) withObject:nil afterDelay:3];
//        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//
//            UIImage *im = self.selectedPhotos.firstObject;
//            CGSize size  = im.size;
//        });
    }
}
- (void)test{
//    UIImage *im = self.selectedPhotos.firstObject;
    
}


#pragma mark -PHPicker

- (void)phPickerAction {
    
//    if (@available(iOS 14, *)) {
//        [CAIPHPickerControllerManager showImagePickerWithDelegate:self withSelectionLimit:4 fromViewController:self];
//    } else {
//        // Fallback on earlier versions
//    }
    if (@available(iOS 14, *)) {
        CAIPHPickerConfiguration *config = [CAIPHPickerConfiguration new];
        config.allowPickingVideo = YES;
        config.selectionLimit = 4;
        self.pickerControllerManager.pickerConfiguration =  config;
        [self.pickerControllerManager showImagePickerWithFromViewController:self delegate:self];

    } else {
        // Fallback on earlier versions
    }
}

- (void)imagePickerControllerManager:(CAIPHPickerControllerManager *)manager didFinishPickingMediaWithPhotos:(NSArray<UIImage *> *)photos error:(NSError *)error API_AVAILABLE(ios(14)){
    
}


#pragma mark -ZXUIImagePickerManagerDelegate,自定义UIImagePickerController

#pragma mark-选择照片
- (void)uiImagePickerPresentPhotoLibrary{
//    self.imagePickerVCManager.alwayCheckAuthorization = NO;
//    self.imagePickerVCManager.allowWriteToSavedPhotos = YES;
//    [self.imagePickerVCManager presentPhotoActionSheetToImagePickerWithSourceController:self];
    self.imagePickerVCManager.allowsEditing = self.uiImagePickerSwitch.isOn;
    [self.imagePickerVCManager presentPhotoLibraryWithSourceController:self];


}

#pragma mark-单拍照片
- (void)uiImagePickerPresentCamera{
    self.imagePickerVCManager.allowsEditing = self.uiImagePickerSwitch.isOn;
    self.imagePickerVCManager.allowWriteToSavedPhotos = YES;
    [self.imagePickerVCManager presentCameraWithSourceController:self allowTakePhoto:YES allowTakeVideo:NO];
}


#pragma mark-自定义多拍照片
- (void)uiImagePickerPresentCameraMultiPhotoAction{
    self.imagePickerVCManager.allowsEditing = self.uiImagePickerSwitch.isOn;
    self.imagePickerVCManager.allowWriteToSavedPhotos = YES;
    self.imagePickerVCManager.watermask = YES;
    [self.imagePickerVCManager presentCameraMultiPhotoWithSourceController:self maxTakePhotoNum:[self.customCameraMaxPhotoTextField.text integerValue]];
    [CAIImagePickerControllerKit setLocation:@"杭州市滨江区长河街道 1866号 阿里中心 长河街道 1866号 阿里中心 长河街道 1866号 阿里中心"];
}

#pragma mark-自定义单拍摄视频
- (void)uiImagePickerCameraMovieAction{
    self.imagePickerVCManager.allowsEditing = self.uiImagePickerSwitch.isOn;
    [self.imagePickerVCManager presentCameraWithSourceController:self allowTakePhoto:NO allowTakeVideo:YES];
}


//展示自定义相机的OverlayView
- (void)showCameraOverlayView{
    CAICameraOverlayView *view = [[CAICameraOverlayView alloc] init];
    view.delegate = self;
    [self.view.window addSubview:view];
}
#pragma mark - CAICameraOverlayViewDelegate

- (void)cameraOverlayViewDidCancel:(CAICameraOverlayView *)view{
    [view removeFromSuperview];
}



- (void)imagePickerControllerManager:(ZXUIImagePickerManager *)manager imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info image:(UIImage *)image error:(NSError *)error{
    
}

- (void)imagePickerControllerManager:(ZXUIImagePickerManager *)manager didFinishPickingImages:(NSArray<UIImage *> *)images imageModels:(NSArray<CAIUIImagePickerCameraPhotoModel *> *)models error:(NSError *)error{
    
}

- (ZXUIImagePickerManager *)imagePickerVCManager
{
    if (!_imagePickerVCManager) {
        ZXUIImagePickerManager *manager = [[ZXUIImagePickerManager alloc] init];
        manager.delegate = self;
        _imagePickerVCManager = manager;
    }
    return _imagePickerVCManager;
}

- (CAIPHPickerControllerManager *)pickerControllerManager API_AVAILABLE(ios(14)){
    
    if (!_pickerControllerManager) {
        _pickerControllerManager = [[CAIPHPickerControllerManager alloc] init];
    }
    return _pickerControllerManager;
}

- (CAIImagePickerControllerKit *)imagePickerControllerKit{
    if (!_imagePickerControllerKit) {
        _imagePickerControllerKit = [CAIImagePickerControllerKit new];
    }
    return _imagePickerControllerKit;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
